package com.chat.datea.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.Toast;

import com.chat.datea.R;
import com.chat.datea.acitivity.ChangeSkinActivity;
import com.chat.datea.acitivity.SettingActivity;

import java.util.Calendar;

/**
 * Created by Shura on 15/10/28.
 */
public class MoreFragment extends Fragment implements View.OnClickListener {

    private View view;
    private ImageView more_back;
    private AlertDialog dialog;
    private SharedPreferences preferences;
    private CheckBox wifi_connect, auto_close, desktop_lyrics, lock_screen;
    private LinearLayout setup_skin, setup_setting;

    private CountDownTimer countDownTimer;

    // 选定关闭时间(以小时为单位)
    int closeHour = 0;

    // 选定关闭时间(以分钟为单位)
    int closeMinutes = 0;
    Calendar calendar = null;
    private Stop stop;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.more_layout, null);
        initView(view);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        stop = (Stop) context;
    }

    public interface  Stop{
        void stop();
    }

    private void initView(View view) {

        more_back = (ImageView) view.findViewById(R.id.more_back);
        wifi_connect = (CheckBox) view.findViewById(R.id.wifi_connect);
        auto_close = (CheckBox) view.findViewById(R.id.auto_close);
        desktop_lyrics = (CheckBox) view.findViewById(R.id.desktop_lyrics);
        lock_screen = (CheckBox) view.findViewById(R.id.lock_screen);

        auto_close.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                alertDialog();
            }
        });

        lock_screen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent intent = new Intent("com.chat.datea.acitivity.LockScreenActivity");
                startActivity(intent);
            }
        });

        setup_skin = (LinearLayout) view.findViewById(R.id.setup_skin);
//        setup_sound = (LinearLayout) view.findViewById(R.id.setup_sound);
//        setup_song = (LinearLayout) view.findViewById(R.id.setup_song);
//        setup_scan = (LinearLayout) view.findViewById(R.id.setup_scan);
//        setup_import = (LinearLayout) view.findViewById(R.id.setup_import);
        setup_setting = (LinearLayout) view.findViewById(R.id.setup_setting);

        more_back.setOnClickListener(this);
        setup_setting.setOnClickListener(this);
        setup_skin.setOnClickListener(this);
    }

    private void alertDialog() {
        final Model model = new Model();
        View view = null;
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        view = inflater.inflate(R.layout.time_picker_alert_dialog, null);
        model.ensureButton = (Button) view.findViewById(R.id.confirm_btn);
        model.timePicker = (TimePicker) view.findViewById(R.id.timePicker);

        // 设置为24小时制
        model.timePicker.setIs24HourView(true);

        // 初始化小时
        model.timePicker.setCurrentHour(0);

        // 初始化分钟
        model.timePicker.setCurrentMinute(1);
        model.cancelButton = (Button) view.findViewById(R.id.cancel_btn);
        dialog = new AlertDialog.Builder(getActivity()).setView(view).create();
        dialog.show();

        //确定按钮执行操作
        model.ensureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 获取用户选定时间
                closeHour = ((model.timePicker.getCurrentHour()) * 3600000);
                closeMinutes = ((model.timePicker.getCurrentMinute()) * 60000);
                final int closeTime = closeHour + closeMinutes;
                Toast.makeText(getActivity(), "程序将在" + (closeTime / 60000) + "分钟后关闭", Toast.LENGTH_SHORT).show();
                countDownTimer = new CountDownTimer(closeTime, 1000) {


                    @Override
                    public void onTick(long millisUntilFinished) {

                        if ((millisUntilFinished / 1000) == closeTime - 1) {
                        } else if ((millisUntilFinished / 1000) == 10) {
                            try {

                                Toast.makeText(getActivity(), "马上时间已到，程序将自动退出", Toast.LENGTH_SHORT).show();
                            } catch (Exception e) {

                            }
                        }
                    }

                    @Override
                    public void onFinish() {
                        stop.stop();
                        System.exit(0);
                    }
                };
                countDownTimer.start();
                dialog.dismiss();
            }
        });

        // 取消按钮退出操作
        model.cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (countDownTimer != null) {
                    countDownTimer.cancel();
                }
                dialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setup_setting:
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.more_back:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
            case R.id.setup_skin:
                Intent intent1 = new Intent(getActivity(), ChangeSkinActivity.class);
                startActivity(intent1);
                break;
        }
    }

    class Model {
        private Button ensureButton;
        private Button cancelButton;
        private TimePicker timePicker;
    }
}
